package com.otp.webapp.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.otp.model.test.instance.TestInstance;
import com.otp.model.test.template.Item;
import com.otp.model.test.template.ResponseScoreOption;
//import com.otp.service.instance.ResponseScoreOptionManager;
import com.otp.webapp.comparators.CompOptionsByObjectId;
import com.otp.webapp.comparators.CompOptionsByOrder;

//@Scope("session")
//@Component("answeredItem") //responseScoreOptionManager does not work
public class AnsweredItem{

	private TestInstance testInstance;
	private Item item;
	private String description;
	private double score;
	private List<String> availableOptions=new ArrayList<String>();
	private List<ResponseScoreOption> availableOptionEntities = new ArrayList<ResponseScoreOption>();
	private String selectedOption;
	private ResponseScoreOption selectedOptionEntity;
	private int selectedOptionIndex;
	private int answerOrder;
	//private ResponseScoreOptionManager responseScoreOptionManager;


	public TestInstance getTestInstance() {
		return testInstance;
	}
	public void setTestInstance(TestInstance testInstance) {
		this.testInstance = testInstance;
	}
	public Item getItem() {
		return item;
	}
	public void setItem(Item item) {
		this.item = item;
	}
	public String getDescription() {
		description=item.getDescription();
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	public List<String> getAvailableOptions() {
		return availableOptions;
	}
	public void setAvailableOptions(List<String> availableOptions) {
		this.availableOptions = availableOptions;
	}
	public List<ResponseScoreOption> getAvailableOptionEntities() {
		return availableOptionEntities;
	}
	public void setAvailableOptionEntities(
			List<ResponseScoreOption> availableOptionEntities) {
		this.availableOptionEntities = availableOptionEntities;
	}
	public String getSelectedOption() {
		return selectedOption;
	}
	public void setSelectedOption(String selectedOption) {
		this.selectedOption = selectedOption;
	}
	public ResponseScoreOption getSelectedOptionEntity() {
		return selectedOptionEntity;
	}
	public void setSelectedOptionEntity(ResponseScoreOption selectedOptionEntity) {
		this.selectedOptionEntity = selectedOptionEntity;
	}
	public int getSelectedOptionIndex() {
		return selectedOptionIndex;
	}
	public void setSelectedOptionIndex(int selectedOptionIndex) {
		this.selectedOptionIndex = selectedOptionIndex;
	}
	public int getAnswerOrder() {
		return answerOrder;
	}
	public void setAnswerOrder(int answerOrder) {
		this.answerOrder = answerOrder;
	}
	//	@Autowired//Make it work !!!
	//	public void setResponseScoreOptionManager(@Qualifier("responseScoreOptionManager")ResponseScoreOptionManager responseScoreOptionManager) {
	//		this.responseScoreOptionManager = responseScoreOptionManager;
	//	}

	public AnsweredItem(){}

	public AnsweredItem(Item item, int order, TestInstance testInstance, List<ResponseScoreOption> options){
		this.testInstance = testInstance;
		this.item = item;
		description = item.getDescription();
		this.answerOrder=order;
		availableOptionEntities = options;
		Collections.sort(availableOptionEntities, new CompOptionsByOrder());		
		for(ResponseScoreOption opt:availableOptionEntities) availableOptions.add(opt.getDescription());		
	}
	
	public void setSelectionAndUpdate(String selection){
		selectedOption=selection;
		updateOnGivenSelection();
	}

	public void updateOnGivenSelection(){
		if(selectedOption==null||selectedOption.isEmpty()) {System.out.println("User answer unknown in AnsweredItem class"); return;} //selectedOption usually set by the front end
		if(availableOptionEntities==null||availableOptionEntities.size()==0) {System.out.println("No available Option Entities"); return;}
		Collections.sort(availableOptionEntities, new CompOptionsByObjectId()); //This is important for the index returned
		boolean foundMatch=false;

		for(int i=0; i<availableOptionEntities.size();i++){
			ResponseScoreOption option = availableOptionEntities.get(i);
			if(option.getDescription().equals(selectedOption)){
				selectedOptionEntity=option;
				selectedOptionIndex=i;
				if(option.getScore()!=null) score=option.getScore();
				foundMatch=true;
				break;
			}
		}
		if(!foundMatch) {System.out.println("No option found that matches the option string given!"); return;}
	}


	public List<Double> getThresholds(){
		if(availableOptionEntities==null||availableOptionEntities.isEmpty()) {System.out.println("No options in method getThresholds"); return null;}
		Collections.sort(availableOptionEntities, new CompOptionsByObjectId());//This is important
		List<Double> b = new ArrayList<Double>();
		for(int j=1; j<availableOptionEntities.size(); j++) b.add(availableOptionEntities.get(j).getDifficultyThreshold());
		for(int j=1; j<b.size(); j++) 
			if(b.get(j)<=b.get(j-1)){
				System.out.println("Thresholds not properly defined in the DB");
				return null;
			}
		return b;
	}

	public String toString(){
		String ans = "\n";
		ans+="options (strings) are:\n";
		for(String str:getAvailableOptions()) ans+=str+"\n";
		ans+="options (reportScoreOptions objects) are:\n";
		for(ResponseScoreOption option:getAvailableOptionEntities()) ans+=option.getDescription()+"\n";
		ans+="selected options is "+getSelectedOption()+"\n";
		return ans;
	}


}
